#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>

// 易变关键字
volatile int flag = 0;

// printBLocklist
void PirintBLock()
{
    sigset_t set, oset;
    sigemptyset(&set);
    sigemptyset(&oset);

    sigprocmask(SIG_BLOCK, &set, &oset);
    std::cout << "block: ";
    for (int signo = 31; signo > 0; signo--)
    {
        if (sigismember(&oset, signo))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}

void PrintPending()
{
    sigset_t pending;
    ::sigpending(&pending);

    std::cout << "Pending: ";
    for (int signo = 31; signo > 0; signo--)
    {
        if (sigismember(&pending, signo))
        {
            std::cout << 1;
        }
        else
        {
            std::cout << 0;
        }
    }
    std::cout << std::endl;
}

void handler(int signo)
{
    static int cnt = 0;
    cnt++;
    while (true)
    {
        std::cout << "get a sig: " << signo << ", cnt: " << cnt << std::endl;
        // PirintBLock();
        PrintPending();
        sleep(1);
        // break;
    }
    // exit(1);
}

int main()
{
    struct sigaction act, oact;
    act.sa_handler = handler;
    sigemptyset(&act.sa_mask);
    // for(int i=1 ;i <= 31 ;++i)
    //     sigaddset(&act.sa_mask, i);


    ::sigaction(2, &act, &oact);

    while (true)
    {
        // PirintBLock();
        PrintPending();
        pause();
    }
}